<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Cate;
use app\admin\model\Category  as modelcate;
use think\Model;
use think\View;
use think\Request;
use app\admin\controller\Base;
class Category extends Base
{
    //展示
    public function Category()
    {
        $cate=model("Cate");
        $res=$cate->index();
       // var_dump($res);die;
       return $this->fetch('category',['info'=>$res]);
    }

    public function addcategory()
    {
        $cate=new Cate();
        $res=$cate->index();
        $cate=new modelcate();
        $data=$cate->createTree($res);
 
        return $this->fetch('addcategory',['data'=>$data]);
    }
    public function del()
    {
        $id=Request::instance()->param("cat_id");
        $user=model("Cate");
        $res=$user->dels($id);
        if ($res) {
            echo "<script>alert('删除成功');location.href='Category'</script>";
        }else{
            echo "<script>alert('删除失败');location.href='show'</script>";
        }
    }
    public function add()
    {
        $post=Request::instance()->param();

        $usere=model("Cate");
        // var_dump($post);

        $res=$usere->adds($post);
        if ($res) {
            echo "<script>alert('添加成功');location.href='category'</script>";
        }else{
            echo "<script>alert('添加失败');location.href='addcategory'</script>";
        }
    }
    public function changeStatus(){

        $cat_id = input('get.cat_id');
        $is_show = input('get.is_show');

        if($is_show==1){

            $res = Db("category")->where("cat_id",$cat_id)->update(["is_show"=>'0']);
        }else{

            $res = Db("category")->where("cat_id",$cat_id)->update(["is_show"=>'1']);
        }

        if($res){

            $arr['status'] = 0;
            $arr['data'] = '';
            $arr['msg'] = '成功';
        }else{

            $arr['status'] = 1;
            $arr['data'] = '';
            $arr['msg'] = '失败';
        }

        echo json_encode($arr);
    }
    public function changeStatus2(){

        $cat_id = input('get.cat_id');
        $show_in_nav = input('get.show_in_nav');

        if($show_in_nav==1){

            $res = Db("category")->where("cat_id",$cat_id)->update(["show_in_nav"=>'0']);
        }else{

            $res = Db("category")->where("cat_id",$cat_id)->update(["show_in_nav"=>'1']);
        }

        if($res){

            $arr['status'] = 0;
            $arr['data'] = '';
            $arr['msg'] = '成功';
        }else{

            $arr['status'] = 1;
            $arr['data'] = '';
            $arr['msg'] = '失败';
        }

        echo json_encode($arr);
    }
}